package commom

import (
	"github.com/garyburd/redigo/redis"
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	"websocket/response"
)

var redisDB redis.Conn

func InitRedis() redis.Conn {
	var c *gin.Context
	var r redis.Conn
	var err error
	r, err = redis.Dial("tcp", viper.GetString("redis.host")+":"+viper.GetString("redis.port"))
	if err != nil {
		response.FailWithServerError("redis连接失败", c)
		return nil
	}
	_, err = r.Do("SELECT", viper.GetString("redis.db"))
	if err != nil {
		response.FailWithServerError("redis创建集合失败", c)
		return nil
	}
	redisDB = r
	return r
}

func GetRedis() redis.Conn {
	return redisDB
}

func GetValues(reply interface{}, err error) ([]interface{}, error) {
	return redis.Values(reply, err)
}
